import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:hkzf_flutter/dio/room_item.dart';
import 'package:hkzf_flutter/pages/search/filter_bar/data.dart';
import 'package:hkzf_flutter/pages/search/filter_bar/index.dart';
import 'package:hkzf_flutter/pages/search/room/room_item.dart';
import 'package:hkzf_flutter/scoped_model/city_model.dart';
import 'package:hkzf_flutter/utils/dio_http.dart';
import 'package:hkzf_flutter/utils/scoped_model_helper.dart';
import 'package:hkzf_flutter/widgets/room_list_item.dart';

// 房源面板组件
class RoomPanel extends StatefulWidget {
  const RoomPanel({super.key});

  @override
  State<RoomPanel> createState() => _RoomPanelState();
}

class _RoomPanelState extends State<RoomPanel> {
  // 渲染数据集合
  List<RoomItem> list = roomList;
  _onFilterBarChae(FilterBarResult data) async{
      var cityId = Uri.encodeQueryComponent(ScopedModelHelper.getModel<CityModel>(context).city?.id ?? '');
      // print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@area:${data.areaId},rentType:${data.rentTypeId},priceId:${data.priceId},more:${data.moreIds}');
      var area =  Uri.encodeQueryComponent(data.areaId ?? '');
      var rentType = Uri.encodeQueryComponent(data.rentTypeId ?? '');
      var price = Uri.encodeQueryComponent(data.priceId ?? '');
      var more = Uri.encodeQueryComponent(data.moreIds?.join(',') ?? '');
      var path = '/houses?cityId=$cityId&area=$area&rentType=$rentType&price=$price&more=$more&start=1&end=100';
      var res =await DioHttp.of(context).get(path);
      List resultList = json.decode(res.toString())['body']['list'];
      List<RoomItem> resList = resultList.map((item) => RoomItem.fromJson(item)).toList();
      setState(() {
        list = resList;
      });
  }

  @override
  Widget build(BuildContext context) {
    // 一个 Column 的父容器没有限制其高度，那么 Column 会无限增长以适应其子组件的高度。
    // Column 布局之所以占据整个屏幕高度(当 Column 被放置在没有明确高度约束的上下文中（如 Scaffold.body），它将根据其内容填充可用空间,最终等于屏幕可视区域的高度)
    return Column(
      children: [
        // 顶部过滤组件
        FilterBar(onChange: (FilterBarResult data) { 
          print('筛选器改变回调:$data');
          _onFilterBarChae(data);
        },
        ),
        Expanded(
          child: ListView(
            children: list.map((item)=>RoomListItem(
              data: item
            )).toList(),
          ),
        )
        
      ],
    );
  }
}